package com.nest4cloud.nest.account.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nest4cloud.nest.account.constant.enums.TransactionsEnums;
import com.nest4cloud.nest.account.entity.AccCategoriesEntity;
import com.nest4cloud.nest.account.entity.AccTransactionsEntity;
import com.nest4cloud.nest.account.entity.AccountsEntity;
import com.nest4cloud.nest.account.mapper.AccTransactionsMapper;
import com.nest4cloud.nest.account.service.AccCategoriesService;
import com.nest4cloud.nest.account.service.AccTransactionsService;
import com.nest4cloud.nest.account.service.AccountsService;
import com.nest4cloud.nest.account.vo.AccountsVo;
import com.nest4cloud.nest.account.vo.accTransaction.*;
import com.nest4cloud.nest.common.security.util.SecurityUtils;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 账单表
 *
 * @author wens
 * @date 2024-11-26 21:14:59
 */
@Service
public class AccTransactionsServiceImpl extends ServiceImpl<AccTransactionsMapper, AccTransactionsEntity> implements AccTransactionsService {

	@Resource
	private AccountsService accountsService;

	@Resource
	private AccCategoriesService accCategoriesService;

	/**
	 * 保存登录用户的交易记录
	 *
	 * @param accTransactions 交易记录实体
	 * @return 保存成功的记录数
	 */
	@Override
	@Transactional(rollbackFor = Exception.class)
	public boolean saveEntity(AccTransactionsEntity accTransactions) {
		accTransactions.setUserId(SecurityUtils.getUser().getId());
		// TODO: 指定账户余额进行扣减或增加
		AccountsEntity accountsServiceById = accountsService.getById(accTransactions.getAccountId());
		if (accountsServiceById == null) {
			throw new IllegalArgumentException("账户不存在");
		}

		String transactionType = accTransactions.getTransactionType();
		// 支出-金额扣减
		BigDecimal balanceDb = accountsServiceById.getBalance();
		if (TransactionsEnums.EXPENSES.getValue().equals(transactionType)) {
			balanceDb = balanceDb.subtract(accTransactions.getAmount());
		} else if (TransactionsEnums.TRANSFER.getValue().equals(transactionType)) {
			// TODO: 转账金额需要进行判断
		} else {
			// 收入-金额增加
			balanceDb = balanceDb.add(accTransactions.getAmount());
		}
		accountsServiceById.setBalance(balanceDb);
		accountsService.updateById(accountsServiceById);
		return this.save(accTransactions);
	}

	/**
	 * 查询账单列表
	 *
	 * @param page    分页参数
	 * @param wrapper 查询条件
	 * @return 分页查询结果
	 */
	@Override
	public Page<AccTransactionsVo> pageEntity(Page page, LambdaQueryWrapper<AccTransactionsEntity> wrapper) {

		Page<AccTransactionsEntity> recordPage = this.page(page, wrapper);
		Page<AccTransactionsVo> resultPage = new Page<>();
		resultPage.setTotal(recordPage.getTotal());
		resultPage.setSize(recordPage.getSize());
		resultPage.setCurrent(recordPage.getCurrent());
		List<AccTransactionsVo> resultList = recordPage.getRecords().stream().map(ele -> {
			AccTransactionsVo vo = new AccTransactionsVo();
			vo.setId(ele.getId());
			vo.setAccountId(ele.getAccountId());
			vo.setAmount(ele.getAmount());
			vo.setTransactionType(ele.getTransactionType());
			vo.setCategoryId(ele.getCategoryId());
			vo.setDate(ele.getDate());
			vo.setDescription(ele.getDescription());
			vo.setCreateTime(ele.getCreateTime());
			// 根据账户ID查询账户信息
			AccountsEntity account = accountsService.getById(ele.getAccountId());
			vo.setAccountName(account.getAccountName());
			// 根据分类ID查询分类
			AccCategoriesEntity categoriesEntity = accCategoriesService.getById(ele.getCategoryId());
			vo.setCategoryName(categoriesEntity.getName());
			return vo;
		}).collect(Collectors.toList());
		resultPage.setRecords(resultList);
		return resultPage;
	}

	/**
	 * 根据资产ID查询消费数据
	 * @param accTransactions 资产实体
	 * @return 消费记录
	 */
	@Override
	public AccTransactionsByAccountVo queryTransactionsByAccount(AccTransactionsEntity accTransactions) {

		AccTransactionsByAccountVo vo = new AccTransactionsByAccountVo();

		// 资产ID
		Long accountId = accTransactions.getAccountId();

		// 查询账户信息
		AccountsEntity account = accountsService.getById(accountId);
		TransactionAccountVo accountsVo = new TransactionAccountVo(accountId, account.getBalance(), account.getAccountName());
		vo.setAccount(accountsVo);


		// 根据资产ID查询本资产按照年月分组的总账信息
		List<MouthTransactions> mouthTransaction = this.baseMapper.queryTotalTransactionsGroupByMouthAccountId(accountId);
		mouthTransaction.forEach(ele -> {
			// 根据资产信息和年月查询-月份下的账单消费信息
			ele.setMouthTransactionsItemList(this.baseMapper.queryTransactionsGroupByMouthAccountId(accountId, ele.getTransactionYearMouth()));
		});
		vo.setMouthTransactions(mouthTransaction);

		return vo;
	}
}
